package Bk.basic;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

public class J19_CdinfoMain {

	public static void main(String[] args) {

		
		
		
	}//main
	
	ArrayCd c = new ArrayCd();
	Scanner sc = new Scanner(System.in);
	
	
	
	
}//class




interface CdinfoService {
	boolean newCd(CdinfoVO c);
	boolean addCd(CdinfoVO c);
	String showCd(CdinfoVO c);
}


class Cdinfo implements CdinfoService {

	private static final String DRV = "oracle.jdbc.OracleDriver";
	private static final String URL = "jdbc:oracle:thin:@//localhost:1521/xe";
	private static final String USR = "scott";
	private static final String PWD = "TIGER";
	
	private static final String createSQL = "create table cdinfo("
			+ "cd_id char(5) primary key, title varchar2(50), publisher varchar2(50),"
			+ "artist varchar2(30), price number(20) ";
	private static final String insertSQL = "insert into cdinfo values('a0001','홍길동 1집',"
			+ "'오늘 레코드','홍길동',8000) ";
	private static final String selectSQL = "select title from cdinfo where price=10000";
	
	@Override
	public boolean newCd(CdinfoVO c) {
		Connection conn = null;
		PreparedStatement ps = null;
		boolean result = false;
		
		
		try{
			Class.forName(DRV);
			conn = DriverManager.getConnection(URL, USR, PWD);
			ps = conn.prepareStatement(createSQL);
			
		}catch (Exception ex) {
			ex.printStackTrace();
		}finally{
			if(ps != null)
				try{ps.close();} catch(Exception ex){}
			if(conn != null)
				try{conn.close();} catch(Exception ex){}
		}
		
		return result;
		
		
	}

	@Override
	public boolean addCd(CdinfoVO c) {
		Connection conn = null;
		PreparedStatement ps = null;
		boolean result = false;
		try{
			Class.forName(DRV);
			conn = DriverManager.getConnection(URL, USR, PWD);
			ps = conn.prepareStatement(insertSQL);
		}catch(Exception ex){
			ex.printStackTrace();
		}finally {
			if(ps != null) try{ps.close();} catch(Exception ex){}
			if(conn != null) try{conn.close();} catch(Exception ex){}
		}
		return result;
	}

	@Override
	public String showCd(CdinfoVO c) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String result = "";
		
		try{
			Class.forName(DRV);
			conn = DriverManager.getConnection(URL, USR, PWD);
			ps = conn.prepareStatement(selectSQL);
			rs = ps.executeQuery();
			
			
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			if(rs != null) try{rs.close();} catch(Exception ex){}
			if(ps != null) try{ps.close();} catch(Exception ex){}
			if(conn != null) try{conn.close();} catch(Exception ex){}
		}
		return result;
	}
	
}

class ArrayCd extends CdinfoVO {
	private CdinfoVO[] cddata = new CdinfoVO[100];
	private int pos = -1;

	public boolean addCd(CdinfoVO c) {
		cddata[ ++pos ] = c;
		return true;
	}
	
	public String showCd() {
		String result = "";
		for(int i =0; i <= pos; ++i) {
			CdinfoVO cd = cddata[i];
			result +=cd.toString() + "\n";
		}
		return result;
	}
	
}



class CdinfoVO {
	private String cd_id; 
	private String title; 
	private String publisher; 
	private String artist; 
	private int price; 
	
	
	public CdinfoVO() {	}


	public CdinfoVO(String cd_id, String title, String publisher, String artist,
			int price) {
		super();
		this.cd_id = cd_id;
		this.title = title;
		this.publisher = publisher;
		this.artist = artist;
		this.price = price;
	}


	public String getCd_id() {
		return cd_id;
	}
	public void setCd_id(String cd_id) {
		this.cd_id = cd_id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getPublisher() {
		return publisher;
	}
	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}
	public String getArtist() {
		return artist;
	}
	public void setArtist(String artist) {
		this.artist = artist;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}


	@Override
	public String toString() {
		return "CdinfoVO [cd_id=" + cd_id + ", title=" + title + ", publisher="
				+ publisher + ", artist=" + artist + ", price=" + price + "]";
	}
	
	
	
	
}





























